﻿using M.Warehouse.API.Application.Command.FinishedProductCommand;
using M.Warehouse.Domain;
using M_ApiResult;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace M.Warehouse.API.Controllers
{
    /// <summary>
    /// 成品控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class FinishedProductController : ControllerBase
    {
        private readonly IMediator _mediator;
        public FinishedProductController(IMediator mediator)
        {
            _mediator = mediator;
        }
        /// <summary>
        /// 添加成品
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> Create(FinishedProductCreateCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Data =await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
         /// 修改成品
         /// </summary>
         /// <param name="command"></param>
         /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> Update(FinishedProductUpdateCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
         /// 删除成品
         /// </summary>
         /// <param name="command"></param>
         /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> Delete([FromQuery]FinishedProductDeleteCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 显示成品
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<List<FinishedProduct>>> Show([FromQuery] FinishedProductShowCommand command)
        {
            ApiResult<List<FinishedProduct>> result = new ApiResult<List<FinishedProduct>>();
            try
            {
                result.Data= await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 返填成品
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<FinishedProduct>> Backfill([FromQuery] FinishedProductBqackfillCommand command)
        {
            ApiResult<FinishedProduct> result = new ApiResult<FinishedProduct>();
            try
            {
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
    }
}
